package com.group.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.group.entity.ProjectInside;
import com.group.entity.ProjectInviteTalents;
import com.group.entity.User;
import com.group.mapper.ProjectInsideMapper;
import com.group.service.ProjectInviteTalentsService;
import com.group.service.UserService;
import com.group.service.impl.GroupsInformationServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 朋心合力
 * @since 2021-09-19
 */
@Controller
public class ProjectInviteTalentsController {

    @Autowired
    private GroupsInformationServiceImpl gfs;

    @Autowired
    ProjectInsideMapper projectInsideMapper;

    @Autowired
    ProjectInviteTalentsService projectInviteTalentsService;

    @Autowired
    UserService userService;

//从前端接收一个组员的id和组的编号
    @RequestMapping("/invite")
    @ResponseBody
    public Integer invite(Integer memberId, Integer groupId) {
        Integer flag = 0;
        ProjectInviteTalents projectInviteTalents = new ProjectInviteTalents();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("groupId", groupId);
        ProjectInside projectInside = projectInsideMapper.selectOne(wrapper);

        QueryWrapper wrapper2 = new QueryWrapper();
        wrapper2.eq("userId", memberId);
        //判断这个id在不在
        User user = userService.getOne(wrapper2);
        if (user == null) {
            flag = 3;
        } else {
            //防止数据冗余
            Map map = new HashMap();
            map.put("groupId", groupId);
            map.put("userId", memberId);
            QueryWrapper wrapper1 = new QueryWrapper();
            wrapper1.allEq(map);
            ProjectInviteTalents one = projectInviteTalentsService.getOne(wrapper1);
            if (one != null) {
                flag = 2;
            } else {
                projectInviteTalents.setProjectId(projectInside.getProjectId());
                projectInviteTalents.setUserId(memberId);
                projectInviteTalents.setGroupId(groupId);
                projectInviteTalentsService.save(projectInviteTalents);
                flag = 1;
            }
        }


        return flag;
    }

    @RequestMapping("/getInviteJoinTeam")
    public String getInviteJoinTeam(HttpSession session, Model model) {
        Integer currentUserId = (Integer) session.getAttribute("currentUserId");
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("userId", currentUserId);
        List inviteList = projectInviteTalentsService.list(wrapper);
        model.addAttribute("inviteList", inviteList);
        return "inviteMe";
    }

    //同意入队
    @RequestMapping("/agreeInvite/{groupId}")
    public String agreeInvite(@PathVariable("groupId") Integer groupId, HttpSession session) throws IllegalAccessException {
        Integer currentUserId = (Integer) session.getAttribute("currentUserId");
        HashMap map = new HashMap();
        map.put("userId", currentUserId);
        map.put("groupId", groupId);
        boolean remove = projectInviteTalentsService.removeByMap(map);
        if (remove) {
            gfs.addGroupMember(groupId, currentUserId);
        }
        return "inviteMe";
    }

    //拒绝入队
    @RequestMapping("/degreeInvite/{groupId}")
    public String degreeInvite(@PathVariable("groupId") Integer groupId, HttpSession session) {
        Integer currentUserId = (Integer) session.getAttribute("currentUserId");
        HashMap map = new HashMap();
        map.put("userId", currentUserId);
        map.put("groupId", groupId);
        projectInviteTalentsService.removeByMap(map);
        return "inviteMe";
    }

}

